//
//  VMBaseTableVM.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/3/30.
//

import Foundation

class APBaseTableVMConfig {

    var isMultipleSection: Bool = false
    
    var isMultipleCell: Bool = false
    
    var cellClass: AnyClass?
    
    var headerClass: AnyClass?
    
    var footerClass: AnyClass?
    
    var headerModel: Any?
    
    var footerModel: Any?
}

class VMBaseTableVM: APTableView_VM_Protocol {
    var config: APBaseTableVMConfig {
        get { _config }
        set {}
    }
    private lazy var _config: APBaseTableVMConfig = {
        let cfg = APBaseTableVMConfig()
        return cfg
    }()
    
    var datasOfCell: [Any]?
    
    var datasOfSection: [Any]?

    func sectionCount() -> Int {
        if config.isMultipleSection {
            return datasOfSection?.count ?? 0
        } else {
            return 1
        }
    }
    
    func numberOfRows(in section: Int) -> Int {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return 0 }
            guard let sectionModel = sectionDatas[vmSafe: section] else { return 0 }
            guard let sVM = sectionModel as? APListViewSection_VM_Protocol else { return 0 }
            guard let datas = sVM.datas else { return 0 }
            return datas.count
        } else {
            guard let datas = datasOfCell else { return 0 }
            return datas.count
        }
    }
    
    func model(at indexPath: IndexPath) -> Any? {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return nil }
            guard let sectionModel = sectionDatas[vmSafe: indexPath.section] else { return nil }
            guard let sVM = sectionModel as? APListViewSection_VM_Protocol, let datas = sVM.datas else { return 0 }
            return datas[vmSafe: indexPath.row]
        } else {
            guard let datas = datasOfCell else { return nil }
            return datas[vmSafe: indexPath.row]
        }
    }
    
    func cellClass(at indexPath: IndexPath) -> AnyClass? {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return nil }
            guard let sectionModel = sectionDatas[vmSafe: indexPath.section] else { return nil }
            guard let sVM = sectionModel as? APListViewSection_VM_Protocol, let datas = sVM.datas else { return nil }
            guard let model = datas[vmSafe: indexPath.row], let m = model as? APReuseView_M_Protocol else { return nil }
            return m.reuseViewClass
        } else {
            guard let datas = datasOfCell else { return nil }
            guard let model = datas[vmSafe: indexPath.row], let m = model as? APReuseView_M_Protocol else { return nil }
            return m.reuseViewClass
        }
    }
    
    //!!!: 可选
    func modelOfReuseHeaderView(in section: Int) -> Any? {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return nil }
            guard let sectionModel = sectionDatas[vmSafe: section] else { return nil }
            guard let sVM = sectionModel as? APListViewSection_VM_Protocol, let m = sVM.headerModel else { return nil }
            return m
        }
        return config.headerModel
    }
    
    func modelOfReuseFooterView(in section: Int) -> Any? {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return nil }
            guard let sectionModel = sectionDatas[vmSafe: section] else { return nil }
            guard let sVM = sectionModel as? APListViewSection_VM_Protocol, let m = sVM.footerModel else { return nil }
            return m
        }
        return config.footerModel
    }
    
    func reuseHeaderViewClass(in section: Int) -> AnyClass? {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return nil }
            guard let sectionModel = sectionDatas[vmSafe: section] else { return nil }
            guard let sm = sectionModel as? APListViewSection_VM_Protocol, let m = sm.headerModel else { return nil }
            return m.reuseViewClass
        }
        return config.headerClass
    }
    
    func reuseFooterViewClass(in section: Int) -> AnyClass? {
        if config.isMultipleSection {
            guard let sectionDatas = datasOfSection else { return nil }
            guard let sectionModel = sectionDatas[vmSafe: section] else { return nil }
            guard let sVM = sectionModel as? APListViewSection_VM_Protocol, let m = sVM.footerModel else { return nil }
            return m.reuseViewClass
        }
        return config.footerClass
    }
}


